/********************************************************/
/*	Project:					*/
/*	Author:		gong_libin			*/
/*	Date:		2016_01_01			*/
/*	File:		GlbBson.cpp			*/
/********************************************************/

#include "GlbBson.h"

CGlbBson::CGlbBson()
{
}

CGlbBson::~CGlbBson()
{
}

bson_t* CGlbBson::GlbBsonGetBson()
{
	return &m_stBson;
}

bson_iter_t* CGlbBson::GlbBsonGetIter()
{
	return &m_stIter;
}

void CGlbBson::GlbBsonInit()
{
	bson_init(&m_stBson);

	return;
}

void CGlbBson::GlbBsonReinit()
{
	bson_reinit(&m_stBson);

	return;
}

void CGlbBson::GlbBsonDestroy()
{
	bson_destroy(&m_stBson);

	return;
}

void CGlbBson::GlbBsonCopy(bson_t* pstBson)
{
	bson_copy_to(pstBson, &m_stBson);

	return;
}

void CGlbBson::GlbBsonClone(CGlbBson& rCBson)
{
	bson_copy_to(rCBson.GlbBsonGetBson(), &m_stBson);
	bson_iter_init(&m_stIter, &m_stBson);

	return;
}

void CGlbBson::GlbBsonFree(void* pMem)
{
	bson_free(pMem);

	return;
}

char* CGlbBson::GlbBsonAsJson(UINT* puiLen)
{
	return bson_as_json(&m_stBson, puiLen);
}

bool CGlbBson::GlbBsonFromJson(char* pszJson, int iJson)
{
	bool bReturn = true;
	bson_error_t stError;

	if (true != (bReturn = bson_init_from_json(&m_stBson, pszJson, iJson, &stError))) {
		GLB__ERROR("%s\n", stError.message);
	}

	return bReturn;
}

char* CGlbBson::GlbBsonIterKey()
{
	return (char*)bson_iter_key(&m_stIter);
}

bool CGlbBson::GlbBsonIterNext()
{
	return bson_iter_next(&m_stIter);
}

bool CGlbBson::GlbBsonIterInit()
{
	return bson_iter_init(&m_stIter, &m_stBson);
}

bson_type_t CGlbBson::GlbBsonIterType()
{
	return bson_iter_type(&m_stIter);
}

bool CGlbBson::GlbBsonIterFind(char* pszKey)
{
	return bson_iter_find(&m_stIter, pszKey);
}

int CGlbBson::GlbBsonGetInt32()
{
	return bson_iter_int32(&m_stIter);
}

bool CGlbBson::GlbBsonGetBool()
{
	return bson_iter_bool(&m_stIter);
}

double CGlbBson::GlbBsonGetDouble()
{
	return bson_iter_double(&m_stIter);
}

long long CGlbBson::GlbBsonGetInt64()
{
	return bson_iter_int64(&m_stIter);
}

char* CGlbBson::GlbBsonGetUtf8(UINT* puiLen)
{
	return (char*)bson_iter_utf8(&m_stIter, puiLen);
}

void CGlbBson::GlbBsonGetDoc(bson_iter_t* pstIter)
{
	UINT uiBuf = 0;
	UCHAR* puszBuf = NULL;

	bson_iter_document(pstIter, &uiBuf, (const uint8_t**)&puszBuf);
	GlbBsonIterStatic(puszBuf, uiBuf);

	return;
}

void CGlbBson::GlbBsonGetArray(bson_iter_t* pstIter)
{
	UINT uiBuf = 0;
	UCHAR* puszBuf = NULL;

	bson_iter_array(pstIter, &uiBuf, (const uint8_t**)&puszBuf);
	GlbBsonIterStatic(puszBuf, uiBuf);

	return;
}

void CGlbBson::GlbBsonPutBool(const char* pszField, bool bValue)
{
	BSON_APPEND_BOOL(&m_stBson, pszField, bValue);

	return;
}

void CGlbBson::GlbBsonPutInt32(const char* pszField, int iValue)
{
	BSON_APPEND_INT32(&m_stBson, pszField, iValue);

	return;
}

void CGlbBson::GlbBsonPutDoc(const char* pszField, bson_t* pstDoc)
{
	BSON_APPEND_DOCUMENT(&m_stBson, pszField, pstDoc);

	return;
}

void CGlbBson::GlbBsonPutUtf8(const char* pszField, char* pszValue)
{
	BSON_APPEND_UTF8(&m_stBson, pszField, pszValue);

	return;
}

void CGlbBson::GlbBsonPutDouble(const char* pszField, double dValue)
{
	BSON_APPEND_DOUBLE(&m_stBson, pszField, dValue);

	return;
}

void CGlbBson::GlbBsonPutArray(const char* pszField, bson_t* pstBson)
{
	BSON_APPEND_ARRAY(&m_stBson, pszField, pstBson);

	return;
}

void CGlbBson::GlbBsonPutInt64(const char* pszField, long long llValue)
{
	BSON_APPEND_INT64(&m_stBson, pszField, llValue);

	return;
}

void CGlbBson::GlbBsonIterStatic(UCHAR* puszBuf, UINT uiBuf)
{
	bson_init_static(&m_stBson, puszBuf, uiBuf);
	bson_iter_init(&m_stIter, &m_stBson);

	return;
}
